package com.example.administrator.exkotlin.ui.list

import android.content.Context
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_list_view.*

/**
 * listView例子
 * */
class ListViewActivity : AppCompatActivity() {

    val listDatas: MutableList<String> = mutableListOf<String>()
    lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list_view)
        //判断是否已经初始化
        if (::context.isLateinit) {
            context = this
        }
        initDatas()
    }

    private fun initDatas() {
        loadDatas()
        val listViewAdapter = ListViewAdapter(listDatas, context)
        lv_list.adapter = listViewAdapter
        btn_fresh.setOnClickListener {
            refreshDatas()
            listViewAdapter.datas = listDatas
            listViewAdapter.notifyDataSetChanged()
        }

//        lv_list.setOnItemClickListener { _, _, position, _ ->
//
//        }

    }

    fun loadDatas() {
        for (cout in 0 until 1000) {
            listDatas.add("" + cout)
        }
    }

    fun refreshDatas() {
        val newDatas = mutableListOf<String>()
        for (listData in listDatas) {
            newDatas.add(listData + "new")
        }
        listDatas.clear()
        listDatas.addAll(newDatas)
    }

    class ListViewAdapter(var datas: List<String>, val mContext: Context?) : BaseAdapter() {

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val view: View
            val viewHolder: ViewHolder
            //如果没有加载过，就初始化，并使用View.tag，viewHolder缓存
            if (convertView == null) {
                view = LayoutInflater.from(mContext).inflate(R.layout.lit_view_item, null)
                val tv_item = view.findViewById<TextView>(R.id.tv_item)
                viewHolder = ViewHolder(tv_item)
                view.tag = viewHolder
            } else {
                view = convertView
                viewHolder = view.tag as ViewHolder
            }
            val item = getItem(position)
            if (item != null) {
                viewHolder.item.text = item
                //设置监听
                viewHolder.item.setOnClickListener {
                    Log.i(javaClass.simpleName, "点击了${item}")
                    Toast.makeText(mContext, "点击了${item}", Toast.LENGTH_SHORT).show()
                }
            }

            return view
        }

        override fun getItem(position: Int): String {
            return datas.get(position)
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int {
            return datas.size
        }

        inner class ViewHolder(val item: TextView)

    }


}
